﻿#region MIT Licenze
//The MIT License

//Copyright (c) 2008 Sebastian Faltoni, dotnetfireball.org

//Permission is hereby granted, free of charge, to any person obtaining a copy
//of this software and associated documentation files (the "Software"), to deal
//in the Software without restriction, including without limitation the rights
//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//copies of the Software, and to permit persons to whom the Software is
//furnished to do so, subject to the following conditions:

//The above copyright notice and this permission notice shall be included in
//all copies or substantial portions of the Software.

//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//THE SOFTWARE.
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.Windows.Markup;

namespace Fireball.OpenXML.Word
{

    /// <summary>
    /// Row
    /// </summary>
    [ContentProperty("TableCells")]
    public class TableRow : OpenXMLEntity, IFindable, IAddChild
    {
        public TableRow()
        {
            this._TableCells = new TableCellCollection(this);
        }

        private Table _OwnerTable = null;


        /// <summary>
        /// Return the owner table of this Row
        /// </summary>
        public Table OwnerTable
        {
            get
            {
                return _OwnerTable;
            }
        }

        internal void SetOwnerTable(Table owner)
        {
            _OwnerTable = owner;
        }

        private TableCellCollection _TableCells = null;


        /// <summary>
        /// The collections of TableCell's of this
        /// Row
        /// </summary>
        public TableCellCollection TableCells
        {
            get
            {
                return _TableCells;
            }
        }

        public override System.Xml.Linq.XContainer ToXml()
        {
            var xRow = new XElement(NS.WM + "tr",
                (from p in this.TableCells select p.ToXml()).ToArray());

            return xRow;
        }

        #region IFindable Members

        public OpenXMLEntity FindByName(string name)
        {
            foreach (var c in this.TableCells)
            {
                if (c.Name == name)
                    return c;

                var cs = c.FindByName(name);

                if (cs != null)
                    return cs;
            }
            return null;
        }

        #endregion

        #region IAddChild Members

        public void AddChild(object value)
        {
            if (!(value is TableCell))
                throw new Exception("Invalid child type a TableCell is required");

            this._TableCells.Add((TableCell)value);
        }

        public void AddText(string text)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}
